package com.ming.sps.server.action;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ming.sps.server.bean.ParamMap;
import com.ming.sps.server.utils.JsonUtil;
import com.ming.sps.server.utils.MingUtil;
import com.ming.sps.server.utils.SqlUtil;

import net.sf.json.JSONObject;

//@Controller
public class QuestionnaireBaseAction extends BaseAction {

    // 满意度调查问卷
    @RequestMapping("questionnaire")
    @ResponseBody
    public String questionnaire() throws Exception {
        String ret = "";
        ret = getQuestionnaire(requestParamByParamMap(getRequest()));
        if (StringUtils.isBlank(ret)) {
            ret = retError("获取不到您的信息！", "operError");
        }
        return ret;
    }

    @RequestMapping("getQuestionnaire")
    private String getQuestionnaire(ParamMap map) throws Exception {
        JSONObject jsonObject = new JSONObject();
        String ret = "";
        ParamMap retMap = new ParamMap();
        retMap.put("flag", false);
        String xh = map.getString("xh");
        ParamMap log = dao.queryOneBySql(
                "select * from z_tm_tableslog where vcstudentno = ? ",
                SqlUtil.constructParam(xh));
        if (log.size() == 0) {
            List<ParamMap> tables = dao
                    .queryListBySql("select * from t_questionnaire_tables order by intseq");
            int dataSize = tables.size();

            if (dataSize > 0) {
                List<ParamMap> pm = new ArrayList<ParamMap>();
                for (ParamMap table : tables) {
                    List<ParamMap> asks = dao.queryListBySql("select vcid,vcanswer,vctablesid,intseq from t_questionnaire_tablesanswer where vctablesid=? order by intseq", SqlUtil
                            .constructParam(table.getString("vcid")));
                    ParamMap map1 = new ParamMap();
                    map1.put("intseq", table.getString("intseq"));
                    map1.put("vcquestion", table.getString("vcquestion"));
                    map1.put("asks", asks);
                    pm.add(map1);
                }
                retMap.put("flag", true);
                jsonObject = JsonUtil.put(jsonObject, "tablesanswer", pm);
                ret = jsonObject.toString();
                ret = MingUtil.encrypt(ret);
            } else {
                ret = retError("获取不到问卷信息！", "operError");
                return ret;
            }
        } else {
            ret = retError("您已参加过问卷调查，无需重复参加！", "attend");
            return ret;
        }
        return ret;
    }

    @RequestMapping("saveQuestionnaire")
    @ResponseBody
    public String saveQuestionnaire() throws Exception {
        JSONObject jsonObject = new JSONObject();
        String answerids = getRequest("answerids", "");
        String xh = getRequest("xh", "");
        String xm = getRequest("xm", "");
        int flag = 0;
        String results[] = answerids.split(",");
        for (String r : results) {
            String sql = "select vcquestion from z_tm_tablesanswer where vcid=? order by intseq";
            ParamMap question = dao.queryOneBySql(sql, SqlUtil.constructParam(r));
            ParamMap p = new ParamMap();
            p.put("vcquestion", question.getString("vcquestion"));
            p.put("vcstudentname", xm);
            p.put("vcstudentno", xh);
            p.put("vcanswerid", r);
            p.put("intresult", "1");
            flag = dao.insert("z_tm_tableslog", p, "vcid");
        }
        jsonObject = JsonUtil.put(jsonObject, "flag", flag);
        return MingUtil.encrypt(jsonObject.toString());
    }
}
